package com.cyt.web.controller.work.associationNew;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.redis.RedisService;
import com.cyt.base.util.AssociationConstant;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.sys.util.EasyPoiUtil;
import com.cyt.web.controller.base.BaseController;
import com.cyt.work.association.dao.WxAssociationNewDao;
import com.cyt.work.association.dao.WxAssociationSignDao;
import com.cyt.work.association.model.po.WxAssociationNew;
import com.cyt.work.association.model.po.WxAssociationNewStu;
import com.cyt.work.association.model.po.WxAssociationSign;
import com.cyt.work.association.model.vo.*;
import com.cyt.work.association.service.WxAssociationNewDetailService;
import com.cyt.work.association.service.WxAssociationNewService;
import com.cyt.work.association.service.WxAssociationNewStuService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 社团招新管理-社团招新管理控制层
 *
 * @author han jun
 * @date 2019-8-30 16:17:54
 */
@RestController
public class WxAssociationNewController extends BaseController {

    @Resource
    private RedisService redisService;
    @Resource
    private WxAssociationNewStuService wxAssociationNewStuService;
    @Resource
    private WxAssociationNewService wxAssociationNewService;


    @Resource
    WxAssociationNewDetailService wxAssociationNewDetailService;

    @Autowired
    private WxAssociationSignDao wxAssociationSignDao;

    @Resource
    private WxAssociationNewDao wxAssociationNewDao;

    /**
     * 统一跳转
     *
     * @param pageName 页面名称
     * @param applyId 应用id
     * @return ModelAndView
     * @author shengQiang Yu
     * @date 2019-08-28 15:17:40
     */
    @RequestMapping("/work/associationNew/wxassociationnew/{pageName}")
    public ModelAndView goPage(@PathVariable("pageName") String pageName, String applyId) {
        ModelAndView view = new ModelAndView("/work/associationNew/wxassociationnew/"+pageName);
        view.addObject("applyId",applyId);
        return view;
    }

    /**
     * 分页查询
     *
     * @param wxAssociationNewVo 接受参数对象
     * @param page 当前页面
     * @param limit 页面数量
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:17:54
     */
    @BaseLog(module = "社团招新管理", classz = "社团招新管理", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/associationNew/wxassociationnew/page", method = RequestMethod.POST)
    public ReturnResult page(WxAssociationNewVo wxAssociationNewVo, int page, int limit) {
        return wxAssociationNewService.page(wxAssociationNewVo, page, limit);
    }

    /**
     * 跳转添加或编辑界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:17:54
     */
    @BaseLog(module = "社团招新管理", classz = "社团招新管理", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/associationNew/wxassociationnew/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(String id,String applyId,String parentMenuId ) {
        ModelAndView view = new ModelAndView("/work/associationNew/wxassociationnew/wxAssociationNewEdit");
        if (StringUtils.isNotBlank(id)) {
            view.addObject(wxAssociationNewService.selectByPrimaryKey(id));
        }
        view.addObject("applyId",applyId);
        view.addObject("id",id);
        view.addObject("parentMenuId",parentMenuId);
        return view;
    }


    /**
     * 跳转详情界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:17:54
     */
    @BaseLog(module = "社团招新管理", classz = "社团招新管理", methods = "跳转详情界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/associationNew/wxassociationnew/getDetail", method = RequestMethod.GET)
    public ModelAndView getDetail(String id,String applyId,String parentMenuId ) {
        ModelAndView view = new ModelAndView("/work/associationNew/wxassociationnew/associationDetail");
        if (StringUtils.isNotBlank(id)) {
            view.addObject(wxAssociationNewService.selectByPrimaryKey(id));
        }
        view.addObject("applyId",applyId);
        view.addObject("id",id);
        view.addObject("parentMenuId",parentMenuId);
        return view;
    }



    /**
     * 跳转招新统计
     *
     * @param id 主键
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:17:54
     */
    @BaseLog(module = "社团招新管理", classz = "社团招新管理", methods = "跳转招新统计界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/associationNew/wxassociationnew/getstatic", method = RequestMethod.GET)
    public ModelAndView getstatic(String id,String applyId) {
        ModelAndView view = new ModelAndView("/work/associationNew/wxassociationnew/associationStatistical");
        if (StringUtils.isNotBlank(id)) {
            view.addObject(wxAssociationNewService.selectByPrimaryKey(id));
        }
        view.addObject("applyId",applyId);
        view.addObject("id",id);
        return view;
    }


    /**
     * 跳转招新人员统计
     *
     * @param id 主键
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:17:54
     */
    @BaseLog(module = "社团招新管理", classz = "社团招新管理", methods = "跳转招新统计界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/associationNew/wxassociationnew/getPeopleStitic", method = RequestMethod.GET)
    public ModelAndView getPeopleStitic(String id,String applyId) {
        ModelAndView view = new ModelAndView("/work/associationNew/wxassociationnew/associationPeopleStiatic");
        if (StringUtils.isNotBlank(id)) {
            view.addObject(wxAssociationNewService.selectByPrimaryKey(id));
        }
        view.addObject("applyId",applyId);
        view.addObject("id",id);
        return view;
    }


    /**
     * 保存或更新
     *
     * @param request 请求对象
     * @param wxAssociationNewVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:17:54
     */
    @BaseLog(module = "社团招新管理", classz = "社团招新管理", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/associationNew/wxassociationnew/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, WxAssociationNewVo wxAssociationNewVo,String associationIds) {
        return wxAssociationNewService.addOrUpdate(wxAssociationNewVo, acceptCurrentUser(request),associationIds);
    }


    /**
     * 保存或更新
     *
     * @param request 请求对象
     * @param wxAssociationNewVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:17:54
     */
    @BaseLog(module = "社团招新管理", classz = "社团招新管理", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/associationNew/wxassociationnew/assocaitionSave", method = RequestMethod.POST)
    public ReturnResult assocaitionSave(HttpServletRequest request, WxAssociationNewVo wxAssociationNewVo) {
        return wxAssociationNewService.associationSave(wxAssociationNewVo, acceptCurrentUser(request));
    }



    /**
     * 设置招新人数
     *
     * @param request 请求对象
     * @param wxAssociationNewVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:17:54
     */
    @BaseLog(module = "社团招新管理", classz = "社团招新管理", methods = "设置招新人数", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/associationNew/wxassociationnew/associationNumberSet", method = RequestMethod.POST)
    public ReturnResult associationNumberSet(HttpServletRequest request, WxAssociationNewVo wxAssociationNewVo,String number) {
        return wxAssociationNewService.associationNumberSet(wxAssociationNewVo,number,acceptCurrentUser(request));
    }



    /**
     * 根据条件逻辑删除
     *
     * @param request 请求对象
     * @param wxAssociationNewVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:17:54
     */
    @BaseLog(module = "社团招新管理", classz = "社团招新管理", methods = "逻辑删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/associationNew/wxassociationnew/remove", method = RequestMethod.GET)
    public ReturnResult remove(HttpServletRequest request, WxAssociationNewVo wxAssociationNewVo) {
        return wxAssociationNewService.remove(wxAssociationNewVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件物理删除
     *
     * @param request 请求对象
     * @param wxAssociationNewVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:17:54
     */
    @BaseLog(module = "社团招新管理", classz = "社团招新管理", methods = "物理对象对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/associationNew/wxassociationnew/delete", method = RequestMethod.GET)
    public ReturnResult delete(HttpServletRequest request, WxAssociationNewVo wxAssociationNewVo) {
        return wxAssociationNewService.delete(wxAssociationNewVo, acceptCurrentUser(request));
    }

    /**
     * 跳转到社团添加的界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:17:54
     */
    @BaseLog(module = "社团招新管理", classz = "社团招新管理", methods = "社团招新添加社元", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/associationNew/wxassociationnew/selectAssociation", method = RequestMethod.GET)
    public ModelAndView selectAssociation(String id,String selectIds,String applyId) {
        ModelAndView view = new ModelAndView("/work/associationNew/wxassociationnew/selectAssociation");
        if (StringUtils.isNotBlank(id)) {
            view.addObject(wxAssociationNewService.selectByPrimaryKey(id));
        }
        view.addObject("selectIds",selectIds);
        view.addObject("applyId",applyId);
        return view;
    }


    /**
     * 跳转到社团招新的人员设置
     *
     * @param id 主键
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:17:54
     */
    @BaseLog(module = "社团招新管理", classz = "社团招新管理", methods = "社团招新添加社元", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/associationNew/wxassociationnew/setAssociationPeople", method = RequestMethod.GET)
    public ModelAndView setAssociationPeople(String id,String associationNewId) {
        ModelAndView view = new ModelAndView("/work/associationNew/wxassociationnew/setAssociationPeople");
        if (StringUtils.isNotBlank(id)) {
            view.addObject(wxAssociationNewService.selectByPrimaryKey(id));
        }
        view.addObject("associationNewId",associationNewId);
        return view;
    }

    /**
     * 招新结束
     *
     * @param request 请求对象
     * @param wxAssociationNewVo 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:17:54
     */
    @BaseLog(module = "社团招新管理", classz = "社团招新管理", methods = "物理对象对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/associationNew/wxassociationnew/endAssociation", method = RequestMethod.GET)
    public ReturnResult endAssociation(HttpServletRequest request, WxAssociationNewVo wxAssociationNewVo) {
        return wxAssociationNewService.endAssociation(wxAssociationNewVo, acceptCurrentUser(request));
    }


    /**
     * 导出招新社团信息
     * @param response
     * @author hanjun
     * @date 2019/9/3 13:43
     */
    @BaseLog(module = "社团招新管理", classz = "社团招新管理", methods = "导出招新社团信息", businessType = BusinessType.EXPORT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/associationNew/wxassociationnew/export")
    public void exportExcel(HttpServletResponse response,String id) {
        List<WxAssociationNewExportVo> list = new ArrayList<>();
        WxAssociationNewDetailVo wxAssociationNewDetailVo = new WxAssociationNewDetailVo();
        wxAssociationNewDetailVo.setAssociationNewId(id);
        List<WxAssociationNewDetailVo> detailList =  wxAssociationNewDetailService.findVoList(wxAssociationNewDetailVo);
        if (detailList.size()>0){
            for (int i=0;i<detailList.size();i++){
                WxAssociationNewDetailVo wxAssociationNewDetail = detailList.get(i);
                list.add(new WxAssociationNewExportVo(String.valueOf(i+1),wxAssociationNewDetail.getTitle(),wxAssociationNewDetail.getTotalNumber(),wxAssociationNewDetail.getStuNumber()));

            }
        }
        WxAssociationNew associationNew = wxAssociationNewDao.selectByPrimaryKey(id);

        EasyPoiUtil.exportExcel(list, associationNew.getTitle(), "社团招新信息", WxAssociationNewExportVo.class, "社团招新信息.xls", response);
    }


    /**
     * 导出社团人员报名信息
     * @param response
     * @author hanjun
     * @date 2019/9/3 13:43
     */
    @BaseLog(module = "社团招新管理", classz = "社团招新管理", methods = "导出招新人员信息", businessType = BusinessType.EXPORT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/associationNew/wxassociationnew/exportStu")
    public void exportStu(HttpServletResponse response,String id) {
        List<WxAssociationNewStuExport> list = new ArrayList<>();

        WxAssociationSignVo wxAssociationSignVo = new WxAssociationSignVo();
        wxAssociationSignVo.setAssociationNewId(id);
        List<WxAssociationSignVo> detailList = wxAssociationSignDao.selectVoHasSignList(wxAssociationSignVo);
        if (detailList.size() > 0) {
            for (int i = 0; i < detailList.size(); i++) {
                WxAssociationSignVo wxAssociationNewDetail = detailList.get(i);
                list.add(new WxAssociationNewStuExport(String.valueOf(i + 1), wxAssociationNewDetail.getAssTitle(), wxAssociationNewDetail.getUserName(), wxAssociationNewDetail.getUserNumber(), wxAssociationNewDetail.getGradeName(), wxAssociationNewDetail.getClassName(), wxAssociationNewDetail.getSignTime().substring(0, 10)));

            }
        }
        WxAssociationNew associationNew = wxAssociationNewDao.selectByPrimaryKey(id);
      EasyPoiUtil.exportExcel(list, associationNew.getTitle(), "社团招新人员信息", WxAssociationNewStuExport.class,"社团招新人员信息.xls",true, response);

    }

    /**
     * 招新人员统计
     *
     * @param request 请求对象
     * @param id 接受参数对象
     * @return ReturnResult
     * @author han jun
     * @date 2019-8-30 16:17:54
     */
    @BaseLog(module = "社团招新管理", classz = "社团招新管理", methods = "招新人员统计", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/work/associationNew/wxassociationnew/associationNewStatis", method = RequestMethod.GET)
    public ReturnResult associationNewStatis(HttpServletRequest request, String id) {
        return new ReturnResult(wxAssociationNewService.associationNewStatis(id));
    }

    /**
     * 设置redis缓存
     */
    @RequestMapping(value = "/work/associationNew/wxassociationnew/syncCache", method = RequestMethod.POST)
    public ReturnResult syncCache() {
        // 查询数据至缓存
        WxAssociationNewVo wxAssociationNewVo = new WxAssociationNewVo();
        wxAssociationNewVo.setStatus("1");
        List<WxAssociationNew> wxAssociationNews = wxAssociationNewService.listExample(wxAssociationNewVo);
        if (CollUtil.isEmpty(wxAssociationNews)) {
            return new ReturnResult();
        }
        // 1、缓存社团信息
        // 2、缓存学生社团信息
        Map<String, List<WxAssociationNewDetailVoC>> map = new HashMap<>();
        // 所有学生
        for (WxAssociationNew wxAssociationNew : wxAssociationNews) {
            WxAssociationSign wxAssociationSign = new WxAssociationSign();
            wxAssociationSign.setAssociationNewId(wxAssociationNew.getId());
            wxAssociationSignDao.delete(wxAssociationSign);
            WxAssociationNewDetailVo wxAssociationNewDetailVo = new WxAssociationNewDetailVo();
            wxAssociationNewDetailVo.setAssociationNewId(wxAssociationNew.getId());
            List<WxAssociationNewDetailVoC> list = wxAssociationNewDetailService.findDetailVoList(wxAssociationNewDetailVo);
            if (CollUtil.isNotEmpty(list)) {
                // 社团数目
                for (WxAssociationNewDetailVoC detail : list) {
                    String detailStr = JSON.toJSONString(detail);
                    redisService.set(AssociationConstant.WX_ASSOCIATION_NEW_DETAIL + detail.getId(), detailStr);
                    redisService.set(AssociationConstant.WX_ASSOCIATION_NEW_DETAIL_NUM + detail.getId(), String.valueOf(detail.getTotalNumber()));
                }
            }
            // 缓存学生信息，1、学生报名社团数据，2、学生报名数目
            WxAssociationNewStuVo wxAssociationNewStuVo = new WxAssociationNewStuVo();
            wxAssociationNewStuVo.setAssociationNewId(wxAssociationNew.getId());
            List<WxAssociationNewStu> stuList = wxAssociationNewStuService.findList(wxAssociationNewStuVo);
            if (CollUtil.isNotEmpty(stuList)) {
                for (WxAssociationNewStu stu : stuList) {
                    // 社团信息，招新，如果是学生
                    if (map.containsKey(stu.getEducationUserId())) {
                        map.get(stu.getEducationUserId()).addAll(list);
                    } else {
                        map.put(stu.getEducationUserId(), list);
                    }
                    // 招新最大报名数
                    redisService.set(AssociationConstant.WX_ASSOCIATION_NEW_STU_NUM + stu.getAssociationNewId() + "_" + stu.getEducationUserId(), wxAssociationNew.getMaxAssoNum());
                }
            }
        }
        // 需要查询历次招新信息，即所有符合条件的信息
        /*for(Map.Entry<String, List<WxAssociationNewDetailVoC>> entry : map.entrySet()){
            String mapKey = entry.getKey();
            List<WxAssociationNewDetailVoC> mapValue = entry.getValue();
            redisService.hset(AssociationConstant.WX_ASSOCIATION_NEW_STU, mapKey, JSONObject.toJSONString(mapValue));
        }*/
        return new ReturnResult();
    }
}